var isVerify = false;
$(function () {
	var code=request("code");
	getSupportCodeLogin();
	if(isNotNull(code))
		if(code=="expiry")
			$("#msg").html("会话已过期，请重新登录");

	/*if ($.cookie(config.cookie.userName)==null || $.cookie(config.cookie.userName)=="undefined"){

	}else{
		window.location.href='./usercenter/index.html';
	}*/

	//会员名
    $("#u_name").focus(function () {
		$("#u_name_msg").attr("class", "login_msg msg2");
		$("#u_name_msg").text("请输入您的会员名");
    }).blur(function () {
		var usrName = $(this).val();
		var objthis = $(this);
		if ($.trim(usrName) == "") {
			$("#u_name_msg").attr("class", "login_msg msg1");
			$("#u_name_msg").text("会员名不能为空");
            return false;
        }
		$("#u_name_msg").attr("class", "");
		$("#u_name_msg").text("");
	});
	//密码
	$("#u_password").focus(function () {
		$("#u_password_msg").attr("class", "login_msg msg2");
		$("#u_password_msg").text("请输入您的密码");
    }).blur(function () {
		 var usrPword = $(this).val();
		   var objthis = $(this);
		  if ($.trim(usrPword) == "") {
			$("#u_password_msg").attr("class", "login_msg msg1");
			$("#u_password_msg").text("密码不能为空");
            return false;
        }
		 $("#u_password_msg").attr("class", "");
		$("#u_password_msg").text("");
	 });
	if (supportCodeLogin == "2") {
		if ("mail" == codeLoginWay) {
            $('.code-login').html("邮箱验证码登录");
		} else {
            $('.code-login').html("手机验证码登录");
		}
		$('.code-login').css('display', '');
	}
});

//登录
function logMember(objs) {
	var usrName = $("#u_name").val();
    var usrPassWord = $("#u_password").val();
    var code = $("#u_code").val();
	if ($.trim(usrName) == "") {
			$("#u_name_msg").attr("class", "login_msg msg1");
			$("#u_name_msg").text("会员名不能为空");
            return false;
        }
	if ($.trim(usrPassWord) == "") {
			$("#u_password_msg").attr("class", "login_msg msg1");
			$("#u_password_msg").text("密码不能为空");
            return false;
        }
	if (code== "" && isVerify) {
		   Commonjs.alert('验证码不能为空');
		   return false;
		}
	var service = {};
	service.userName = usrName;
	service.password = cloudEncrypt.encodePublic(usrPassWord);
	service.checkcode = code;
	var fn="login";
	service = Commonjs.jsonToString(service)
	var params = Commonjs.getParams(fn,service);//获取参数

	$.ajax({
		datatype:"json",
		type:"POST",
		url: weburl,
		data:params,
		async: true,
		timeout : 8000,
		cache : false,
		success: function(obj){
			data =jQuery.parseJSON(obj);
			if(data.result == "success"){
				if(data.msg != "已登录，无需再登录"){
					var cookietime = new Date();
					var date = new Date();
					cookietime.setTime(date.getTime() + (360 * 360 * 1000));//coockie保存1小时
					$.cookie(config.cookie.userName,data.data.userName,{expires: cookietime,path:"/"});
					window.location.href='usercenter/index.html';
				}else{
					Commonjs.alert("请勿重复登陆");
				}
			}else
			{
				refreshVcode();
				$("#u_password").val('');
				$("#u_code").val('')
				if(data.result=="S210")
				{
					$.session.set("reguid",data.data.userId);
					Commonjs.alerturl(data.msg+",点击确定,重新激活帐号","register/regactivity.html");
				}else{
					Commonjs.alert(data.msg);
					return false;
				}
			}
		}
	});
}

function getSupportCodeLogin(){
    var self = this;
    var paramEName="supportCodeLogin";
    var service = {};
    var fn = "getparam";
    service.code = paramEName;
    service = Commonjs.jsonToString(service);
    var params = Commonjs.getParams(fn, service);//获取参数
    Commonjs.ajaxTrue(sysurl,params,function (data) {
        if(data.data.length > 0){
           if(data.data[0].paramValue == 2){
                $(".verify_show").css("display","block");
                isVerify = true;
           }else{
           		$(".verify_show").css("display","none");
           		isVerify = false;
           } 
        }
    },false);
}
//刷新验证码
refreshVcode();
var divUserLogin = new Vue({
    el: '#hl-body',
    data: function() {
    	return {
    		dialogTableVisible:true,
            outerVisible: false,
            innerVisible: false,
            openId:'',
            thirdParam:{},
            cloudMessage: null,
            sourceUrl: '',
            bindVisible:false,
            form: {
                userName: '',
                password: '',
                randCode: ''
            },
            personForm:{
                userName:'',
                password:'',
                mobile:'',
                email:'',
                userType:1,
                loginType:2,
                type:2
            },
            formLabelWidth:90,
            labelPosition: 'right',
            openId:'',
            isBind:false,
            loginUrl:'',
            isShowAuth:false
    	}
    },
    methods: {
    	getThirdParamList(){
            var self = this;
            var service = {};
            var fn = "getQQParam";
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(weburl,params,function (data) {
            	if(data.code == 'S000'){
            		self.thirdParam = data.data;
	                self.loginUrl = self.thirdParam.authParamUrl;
	                self.isShowAuth = self.thirdParam.openQqLogin == 'Y'?true:false;
	                self.makeListen();
            	}
                
            },false);
        },
        sendAuthMessage:function(){
            var self = this;
            var messageType = 'thirdAuthMessageResponse';    
            self.cloudMessage.send(document.getElementById('thirdAuth'), messageType, self.thirdParam.param);
        },

        qqAuthLogin(){
            this.outerVisible = true;
            var loginUrl = this.loginUrl;
            var timestamp=new Date().getTime();
            if(loginUrl.indexOf("?") == -1){
                loginUrl += "?time="+timestamp;
            }else{
                var len = loginUrl.indexOf("?");
                loginUrl = loginUrl.substr(0,len)+"?time="+timestamp;
            }
            $("#thirdAuth").attr("src",loginUrl);  
            // this.sendAuthMessage();
        },
        makeListen:function(){
            var self = this;
            //添加监听
            this.cloudMessage = new CloudMessage();
            this.cloudMessage.addListen('thirdAuthOpenIdResponse', function (message) {
                //接收第三方登录url信息
                var service = {};
                var fn = "authThirdLogin";
                self.openId = message.openId;
                service.openId = message.openId;
                service.loginType = 2;
                service = Commonjs.jsonToString(service);
                var params = Commonjs.getParams(fn, service);//获取参数
                
                // self.bindVisible = true;
                Commonjs.ajaxTrue(weburl,params,function (data) {
                    self.handleClose();
                    // self.thirdParam = data.data;
                    if(data.code == 'S000'){
                        self.$message({
                            message: '登录成功',
                            type: 'success'
                        });
                        var cookietime = new Date();
                        var date = new Date();
                        cookietime.setTime(date.getTime() + (360 * 360 * 1000));//coockie保存1小时
                        $.cookie(config.cookie.userName,data.data.userName,{expires: cookietime,path:"/"});
                        window.location.href=loader.path('usercenter/index.html');
                    }else if(data.code == 'S305'){
                        self.openBindVisiable();
                    }else{
                        self.$message.error('登录失败');
                    }
                    
                },false);

            }).addListen('loadCompleteInfo', function (data) {
                //接收第三方登录url信息
                self.sendAuthMessage();
            });
            // self.sourceUrl = self.cloudMessage.getSourceUrl();
            // self.cloudMessage.startListen(self.sourceUrl);
            self.cloudMessage.startListen(Commonjs.getHostUrlFromString(self.loginUrl));
            // self.cloudMessage.startListen("http://www.yirx.net");
        },
        openBindVisiable:function(){
            this.bindVisible = true;
        },
        bindingAccount:function(){
            var self = this;
            var service = {};
            var fn = "bindThirdAccount";
            service.openId = self.openId;
            service.type = self.isBind ?1:2;
            service.userName = self.personForm.userName;
            service.password = self.personForm.password;
            service.userType = self.personForm.userType;
            service.mobile = self.personForm.mobile;
            service.email = self.personForm.email;
            service.loginType = 2;
            service = Commonjs.jsonToString(service);
            var params = Commonjs.getParams(fn, service);//获取参数
            Commonjs.ajaxTrue(weburl,params,function (data) {
                self.bindVisible = false;
                if(data.code == 'S000'){
                    self.$message({
                        message: '绑定成功',
                        type: 'success'
                    });
                    var cookietime = new Date();
                    var date = new Date();
                    cookietime.setTime(date.getTime() + (360 * 360 * 1000));//coockie保存1小时
                    $.cookie(config.cookie.userName,data.data.userName,{expires: cookietime,path:"/"});
                    window.location.href=loader.path('usercenter/index.html');
                }else{
                    self.$message.error(data.msg);
                    return false;
                }
                
            },false);
        },
        handleClose:function(){
            this.outerVisible = false;
        }
    },
    mounted: function () {
       this.getThirdParamList();
       
    }
});


